<?php

define('IN_API', true);
require_once '../../framework/bootstrap.inc.php';
require_once 'defines.php';
load()->model('mc');
load()->func('communication');
$id = $_GPC['id'];
$_W["account"] = uni_fetch($id);
$account_api = WeAccount::create();
$token = $account_api->getAccessToken();
$settings = pdo_get("uni_account_modules", array("module" => MODULE_NAME , "uniacid" => $id));
$settings = unserialize($settings['settings']);

$condition = "uniacid = {$id} AND s_1_num > 0";
$orders = " ORDER BY user_id DESC";
$list = pdo_fetchall("SELECT * FROM ".tablename(MODULE_NAME."_user")." WHERE ".$condition.$orders);

foreach($list as $k=>$v){
	$url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$token;
	$dd = array();
	$dd['touser'] = $v['openid'];
	$dd['template_id'] = $settings["warn_yhq_tplid"];
	$dd['page'] = 'pages/index/index';
	$dd['data'] = array(
		$settings["warn_yhq_tplid_1"]=>array(
			"value"=>$settings["warn_yhq_tplid_1_text"]
		),
		$settings["warn_yhq_tplid_2"]=>array(
			"value"=>$settings["warn_yhq_tplid_2_text"]
		),
	);
	$res = ihttp_request($url,json_encode($dd));
	$res = json_decode($res["content"],true);
	if($res['errcode'] == 0){
		if($settings['system_type'] == 1){
			$up = pdo_update(MODULE_NAME."_user_u", array("s_1_num -=" => 1), array("unionid" => $v['unionid'], "system_id" => $settings['system_id'],"s_1_num >" => 0));
			$up = pdo_update(MODULE_NAME."_user", array("s_1_num -=" => 1), array("openid" => $v['openid'], "uniacid" => $id,"s_1_num >" => 0));
		}else{
			$up = pdo_update(MODULE_NAME."_user", array("s_1_num -=" => 1), array("openid" => $v['openid'], "uniacid" => $id,"s_1_num >" => 0));
		}
	}else{
		$log = pdo_insert(MODULE_NAME . "_systemlog", array("uniacid" => $id, "api_appKey" => '小程序点餐提醒',"apiname" => 'subscribemessage',"create_time" => $_W["timestamp"], "log" => json_encode($dd).json_encode($res)));
		if($settings['system_type'] == 1){
			$up = pdo_update(MODULE_NAME."_user_u", array("s_1_num" => 0), array("unionid" => $v['unionid'], "system_id" => $settings['system_id'],"s_1_num >" => 0));
			$up = pdo_update(MODULE_NAME."_user", array("s_1_num" => 0), array("openid" => $v['openid'], "uniacid" => $id,"s_1_num >" => 0));
		}else{
			$up = pdo_update(MODULE_NAME."_user", array("s_1_num" => 0), array("openid" => $v['openid'], "uniacid" => $id,"s_1_num >" => 0));
		}
	}
}


if($settings['gzh_diancan_tplid']){
	$start_time = microtime(true);
	$condition = "uniacid = {$settings['gzh_id']}";
	$orders = " ORDER BY uid DESC";
	$list = pdo_fetchall("SELECT * FROM ".tablename("mc_mapping_fans")." WHERE ".$condition.$orders);
	foreach($list as $k=>$v){
		$url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token=" . $token;
		$post = array();
		$post["touser"] = $v['openid'];
		$mp_template_msg = array("appid" => $settings["gzh_appid"], "template_id" => $settings["gzh_diancan_tplid"], "miniprogram" => array("appid" => $_W["account"]["key"], "pagepath" => "pages/index/index"), "data" => array("first" => array("value" => $settings["gzh_diancan_tplid_1"], "color" => "#173177"), "keyword1" => array("value" => $v['nickname'], "color" => "#173177"), "keyword2" => array("value" => $settings["gzh_diancan_tplid_2"], "color" => "#173177"), "keyword3" => array("value" => $settings["gzh_diancan_tplid_3"], "color" => "#173177"), "remark" => array("value" => $settings["gzh_diancan_tplid_4"], "color" => "#173177")));
		$post["mp_template_msg"] = $mp_template_msg;

		$params = json_encode($post,JSON_UNESCAPED_UNICODE);
		
		$fp = fsockopen('ssl://api.weixin.qq.com', 443, $error, $errstr, 1);
		$http = "POST /cgi-bin/message/wxopen/template/uniform_send?access_token={$token} HTTP/1.1\r\nHost: api.weixin.qq.com\r\nContent-type: application/x-www-form-urlencoded\r\nContent-Length: " . strlen($params) . "\r\nConnection:close\r\n\r\n$params\r\n\r\n";
		fwrite($fp, $http);
		fclose($fp);
	}
	print_r(microtime(true) - $start_time);
}







function diancanNotify($openid, $id,$nickname)
{
	global $_GPC;
	global $_W;
	$settings = pdo_get("uni_account_modules", array("module" => MODULE_NAME , "uniacid" =>$id));
	$config = unserialize($settings['settings']);
	load()->model('mc');
	if (!empty($id)) {
		$_W["account"] = uni_fetch($id);
	}
	if (empty($_W["account"])) {
		exit("initial error hash or id");
	}
	$account_api = WeAccount::create();
	$token = $account_api->getAccessToken();
	$url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token=" . $token;
	$post = array();
	$post["touser"] = $openid;
	$mp_template_msg = array("appid" => $config["gzh_appid"], "template_id" => $config["gzh_diancan_tplid"], "miniprogram" => array("appid" => $_W["account"]["key"], "pagepath" => "pages/index/index"), "data" => array("first" => array("value" => $config["gzh_diancan_tplid_1"], "color" => "#173177"), "keyword1" => array("value" => $nickname, "color" => "#173177"), "keyword2" => array("value" => $config["gzh_diancan_tplid_2"], "color" => "#173177"), "keyword3" => array("value" => $config["gzh_diancan_tplid_3"], "color" => "#173177"), "remark" => array("value" => $config["gzh_diancan_tplid_4"], "color" => "#173177")));
	$post["mp_template_msg"] = $mp_template_msg;
	$res = ihttp_request($url, json_encode($post));
	return true;
}

function neworderNotify($openid,$id)
{
	global $_GPC;
	global $_W;
	$settings = pdo_get("uni_account_modules", array("module" => MODULE_NAME , "uniacid" =>$id));
	$config = unserialize($settings['settings']);
	load()->model('mc');
	if (!empty($id)) {
		$_W["account"] = uni_fetch($id);
	}
	if (empty($_W["account"])) {
		exit("initial error hash or id");
	}
	$account_api = WeAccount::create();
	$token = $account_api->getAccessToken();
	$url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$token;
	$dd = array();
	$dd['touser'] = $openid;
	$dd['template_id'] = $config["warn_yhq_tplid"];
	$dd['page'] = 'pages/index/index';
	$dd['data'] = array(
		$config["warn_yhq_tplid_1"]=>array(
			"value"=>$config["warn_yhq_tplid_1_text"]
		),
		$config["warn_yhq_tplid_2"]=>array(
			"value"=>$config["warn_yhq_tplid_2_text"]
		),
	);
	load()->func('communication');
	$res = ihttp_request($url,json_encode($dd));
	$res = json_decode($res["content"],true);
	if($res['errcode'] == 0){
		return true;
	}else{
		return false;
	}
	
}


function signNotify($openid,$id)
{
	global $_GPC;
	global $_W;
	$settings = pdo_get("uni_account_modules", array("module" => MODULE_NAME , "uniacid" =>$id));
	$config = unserialize($settings['settings']);
	load()->model('mc');
	if (!empty($id)) {
		$_W["account"] = uni_fetch($id);
	}
	if (empty($_W["account"])) {
		exit("initial error hash or id");
	}
	$account_api = WeAccount::create();
	$token = $account_api->getAccessToken();
	$url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$token;
	$dd = array();
	$dd['touser'] = $openid;
	$dd['template_id'] = $config["warn_qiandao_tplid"];
	$dd['page'] = 'pages/shop/task';
	$dd['data'] = array(
		$config["warn_qiandao_tplid_1"]=>array(
			"value"=>$config["warn_qiandao_tplid_1_text"]
		),
		$config["warn_qiandao_tplid_2"]=>array(
			"value"=>$config["warn_qiandao_tplid_2_text"]
		),
	);
	load()->func('communication');
	$res = ihttp_request($url,json_encode($dd));
	$res = json_decode($res["content"],true);
	if($res['errcode'] == 0){
		return true;
	}else{
		return false;
	}
	
}